package com.example;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.BeanUtilsBean;
import org.apache.commons.beanutils.ConvertUtilsBean;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.digester3.Digester;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.commons.lang3.tuple.MutablePair;
import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

/**
 * Hello world!
 */
public class MainTest {

    Logger logger = Logger.getLogger(getClass().getName());

    /***
     * CopyProperties方法测试
     * @throws Exception
     */
    @Test
    public void testCopyProperties() throws Exception {
        User user = new User("1", "zhangsan");
        user.getAdddresses().add(new Adddress("1", "湖北"));
        user.getAdddresses().add(new Adddress("2", "湖南"));
        User user1 = new User();
        BeanUtils.copyProperties(user1, user);
        logger.warning(user1.toString());
    }

    /***
     * 给对象赋值
     *
     * @throws Exception
     */
    @Test
    public void testCopyProperty() throws Exception {
        User user = new User();
        BeanUtils.copyProperty(user, "id", "1");
        logger.warning(user.toString());
    }

    /***
     * 获取对象的属性
     *
     * @throws Exception
     */
    @Test
    public void testGetProperty() throws Exception {
        User user = new User("1", "zhangsan");
        user.getAdddresses().add(new Adddress("1", "湖北"));
        logger.warning(BeanUtils.getProperty(user, "adddresses"));
    }

    /***
     * 初始化对象
     *
     * @throws Exception
     */
    @Test
    public void testPopulate() throws Exception {
        User user = new User();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", "1");
        BeanUtils.populate(user, map);
        logger.warning(user.toString());
    }

    /***
     *
     * 把对象属性转成map
     *
     * @throws Exception
     */
    @Test
    public void testDescribe() throws Exception {
        User user = new User("1", "zhangsan");
        Map<String, String> describe = BeanUtils.describe(user);
        for (String key : describe.keySet()) {
            logger.warning("key: " + key + " val: " + describe.get(key));
        }
    }

    /***
     * 测试转换值
     *
     * @throws Exception
     */
    @Test
    public void testConvert() throws Exception {
        ConvertUtilsBean convertUtils = BeanUtilsBean.getInstance().getConvertUtils();
        Object convert = convertUtils.convert(11.2, Integer.class);
        logger.warning(convert.toString());
    }

    /***
     * 测试字符串为空
     *
     * @throws Exception
     */
    @Test
    public void testIsBlank() throws Exception {
        boolean blank = StringUtils.isBlank("");
        logger.warning("blank: " + blank);
    }

    /***
     *
     *  测试是否为空
     * @throws Exception
     */
    @Test
    public void testIsEmpty() throws Exception {
        boolean isEmpty = StringUtils.isEmpty(null);
        logger.warning("isEmpty: " + isEmpty);
    }

    /***
     *
     * @throws Exception
     */
    @Test
    public void testNextInt() throws Exception {
        logger.warning("val: " + RandomUtils.nextInt(100, 200));
    }

    /***
     * 数组复制
     * @throws Exception
     */
    @Test
    public void testClone() throws Exception {
        int[] arrays = new int[]{23, 12, 54, 6};
        for (int i : ArrayUtils.clone(arrays)) {
            logger.warning("index: " + i);
        }
    }

    /***
     * 数组加入对象
     * @throws Exception
     */
    @Test
    public void testAdd() throws Exception {
        int[] arrays = new int[]{23, 12, 54, 6};
        for (int i : ArrayUtils.add(arrays, 100)) {
            logger.warning("index: " + i);
        }
    }

    /***
     * 数组加入对象
     * @throws Exception
     */
    @Test
    public void testAddFirst() throws Exception {
        int[] arrays = new int[]{23, 12, 54, 6};
        for (int i : ArrayUtils.addFirst(arrays, 100)) {
            logger.warning("index: " + i);
        }
    }

    /***
     * 数组加入对象
     * @throws Exception
     */
    @Test
    public void testAddAll() throws Exception {
        int[] arrays1 = new int[]{23, 12, 54, 6};
        int[] arrays2 = new int[]{60, 70};
        for (int i : ArrayUtils.addAll(arrays1, arrays2)) {
            logger.warning("index: " + i);
        }
    }

    /****
     * class工具类
     * @throws Exception
     */
    @Test
    public void testDateUtil() throws Exception {
        Date date = DateUtils.addDays(new Date(), 1);
        logger.warning("date: " + DateFormatUtils.format(date, "yyyy-MM-dd HH:mm:ss"));
    }

    /***
     * 读取文件的内容
     * @throws Exception
     */
    @Test
    public void testReadFileToByteArray() throws Exception {
        File file = new File("H:\\ebs\\settings-v4.xml");
        byte[] bytes = FileUtils.readFileToByteArray(file);
        logger.warning("文件内容: " + new String(bytes));
    }

    /***
     * 判断文件是否被损坏
     * @throws Exception
     */
    @Test
    public void testChecksumCRC32() throws Exception {
        File file = new File("H:\\ebs\\settings-v4.xml");
        long hashCode = FileUtils.checksumCRC32(file);
        logger.warning("文件CRC32值: " + hashCode);
    }

    /***
     * 字符串包裹
     * @throws Exception
     */
    @Test
    public void testWrap() throws Exception {
        logger.warning("warp: " + StringUtils.wrap("adb", "&"));
    }

    /***
     * 多值返回
     * @throws Exception
     */
    @Test
    public void testMutablePair() throws Exception {
        User user = new User();
        Adddress adddress = new Adddress();
        MutablePair mutablePair = MutablePair.of(user, adddress);
    }

    /****
     * 计算文件的md5的值
     * @throws Exception
     */
    @Test
    public void testMd5() throws Exception {
        File file = new File("H:\\ebs\\settings-v4.xml");
        String md5 = DigestUtils.md5Hex(new FileInputStream(file));
        String sha256 = DigestUtils.sha256Hex(new FileInputStream(file));
        logger.warning("md5: " + md5 + " sha256: " + sha256);
    }

    /***
     * 解析XML的内容
     * @throws Exception
     */
    @Test
    public void testXmlDigester() throws Exception {
        Digester digester = new Digester();
        digester.addObjectCreate("Person", Person.class);
        digester.addBeanPropertySetter("Person/Name", "name");
        digester.addBeanPropertySetter("Person/Age", "age");
        InputStream inputStream = getClass().getClassLoader().getResourceAsStream("person.xml");
        Person person = digester.parse(inputStream);
        logger.warning(person.toString());
    }
}